package copyengine.ui.component.button
{
    import copyengine.ui.component.button.animation.ICESelectableButtonAnimation;

    import starling.display.DisplayObject;

    public class CESelectableButton extends CEButton
    {
        private var mIsSelected:Boolean;
        private var mIsClickToSelected:Boolean;
        private var mIsFirstTimeSetSelectProperty:Boolean;

        public function CESelectableButton(_targetMc:DisplayObject) {
            mIsFirstTimeSetSelectProperty = true;
            super(_targetMc);
        }

        /**
         * 取得当前按钮选择状态
         */
        public function get isSelected():Boolean { return mIsSelected; }

        /**
         * 设置当前按钮选择状态
         *
         * 第一次设置isSelected参数时候需要忽略是否和上次一样,
         * 否则无法调用到selectableAnimation.onSelectedChange
         */
        public function set isSelected(_val:Boolean):void {
            if (mIsFirstTimeSetSelectProperty || mIsSelected != _val) {
                mIsFirstTimeSetSelectProperty = false;
                mIsSelected = _val;
                selectableAnimation.onSelectedChange(mIsSelected);
            }
        }

        /**
         * 如果改值为True则表示每次点击会自动更改按钮的选择状态
         */
        public function get isClickToSelect():Boolean { return mIsClickToSelected; }

        /**
         * 如果改值为True则表示每次点击会自动更改按钮的选择状态
         */
        public function set isClickToSelect(_val:Boolean):void { mIsClickToSelected = _val; }

        override protected function doOnClickButton():void {
            if (mIsClickToSelected) {isSelected = !isSelected;}
        }

        //========================//
        //==Private  Function
        //=========================//

        private function get selectableAnimation():ICESelectableButtonAnimation {
            return mAnimation as ICESelectableButtonAnimation;
        }

    }
}
